{extend name="adminblue/base" /}
{block name="resources"/}
<script src="ADMIN_JS/art_dialog.source.js"></script>
<script src="ADMIN_JS/iframe_tools.source.js"></script>
<script src="ADMIN_JS/material_managedialog.js"></script>
<script type="text/javascript" charset="utf-8" src="ADMIN_JS/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="ADMIN_JS/ueditor/ueditor.all.common.js"></script>
<link rel="stylesheet" type="text/css" href="ADMIN_CSS/defau.css">
<style type="text/css">

</style>
{/block}
{block name="main"}
<div class="set-style">
	<h4>常规信息</h4>
	<dl>
		<dt><span class="required">*</span>文章标题：</dt>
		<dd>
			<input id="title" class="input-common long" type="text" name="title" />
			<p class="error">请输入文章标题</p>
		</dd>
	</dl>
	<dl>
		<dt>文章短标题：</dt>
		<dd>
			<input id="short_title" class="input-common" type="text" />
			<p class="error"></p>
		</dd>
	</dl>
	<dl>
		<dt>文章标签：</dt>
		<dd>
			<input id="tag" class="input-common" type="text" />
			<p class="error"></p>
		</dd>
	</dl>
	<dl>
		<dt>文章标题图片：</dt>
		<dd>
			<div class="upload-btn-common">
				<div>
					<input class="input-file" name="file_upload" id="uploadTitleImg" type="file" onchange="imgUpload(this);" title="上传">
					<input type="hidden" id="title_img" value="">
				</div>
				
				<input type="text" id="text_title_img" class="input-common" readonly="readonly" value="">
				<em>上传</em>								
				<img id="preview_title_img" src="__STATIC__/blue/img/upload-common-select.png" data-src="" data-html="true" data-container="body" data-placement="top" data-trigger="manual" data-original-title="" title="">
			</div>
			<p class="hint">
				建议使用<i class="important-note">宽200</i>像素-<i class="important-note">高60</i>像素内的<i class="important-note">GIF</i>或<i class="important-note">PNG</i>透明图片
			</p>
			<p class="error">请上传图片</p>
		</dd>
	</dl>		
	<dl>
		<dt>文章作者：</dt>
		<dd>
			<input id="author" class="input-common" type="text" name="author"/>
			<p class="error"></p>
		</dd>
	</dl>
	<dl>
		<dt>文章来源：</dt>
		<dd>
			<input id="source" class="input-common long" type="text"/>
			<p class="error"></p>
		</dd>
	</dl>
	<dl>
		<dt>文章来源链接：</dt>
		<dd>
			<input id="url" class="input-common" type="text"/>
			<p class="error"></p>
		</dd>
	</dl>
	<dl>
		<dt><span class="required">*</span>文章分类：</dt>
		<dd>
			<select id="class_id" class="select-common">
				<option value="0">请选择</option>
				{volist name="articleClassList['data']" id="vo"}
					{if condition="$vo.pid eq 0"}
					<option value="{$vo.class_id}">{$vo.name}</option>
					{volist name="articleClassList['data']" id="v1"}
						{if condition="$v1.pid eq $vo.class_id"}
						<option value="{$v1.class_id}">&nbsp;&nbsp;&nbsp;&nbsp;{$v1.name}</option>
						{/if}
					{/volist} 
					{/if}
				{/volist}
			</select>
			<p class="error">请选择分类</p>
		</dd>
	</dl>
	<dl>
		<dt>文章关键字：</dt>
		<dd>
			<input id="keyword" type="text" class="input-common"/>
			<p class="error"></p>
		</dd>
	</dl>
	<dl>
		<dt>文章摘要：</dt>
		<dd>
			<textarea id="summary" rows="10" cols="10" class="textarea-common"></textarea>
			<p class="error"></p>
		</dd>
	</dl>
	<h4>文章内容</h4>
	<dl>
		<dt>文章内容：</dt>
		<dd>
			<script id="editor" type="text/plain" style="height: 400px;width:750px;"></script>
			<p class="error"></p>
		</dd>
	</dl>
	<dl>
		<dt>文章附件：</dt>
		<dd>
			<div class="upload-btn-common">
				<div>
					<input class="input-file" name="file_upload" id="uploadAttachment" type="file" onchange="imgUpload(this);" title="上传" only-type=".zip">
					<input type="hidden" id="attachment" value="">
				</div>
				
				<input type="text" id="text_attachment" class="input-common" readonly="readonly" value="">
				<em>上传</em>								
			</div>
			<p class="hint">上传附件类型必须是<i class="important-note">zip</i>格式</p>
			<p class="error"></p>
		</dd>
	</dl>
	<dl class="hide">
		<dt>相关文章：</dt>
		<dd>
			<input id="article_id_array" class="input-common" type="text"/>
			<p class="hint"></p>
			<p class="error"></p>
		</dd>
	</dl>
	<h4>文章阐述</h4>
	<dl>
		<dt>是否推荐：</dt>
		<dd>
			<p>
				<input id="commend_flag" type="checkbox" onchange="setIntegralAjax();" class="checkbox" />
			</p>
			<p class="error"></p>
		</dd>
	</dl>
	<dl>
		<dt>是否允许评论：</dt>
		<dd>
			<p>
				<input id="comment_flag" type="checkbox"  class="checkbox" />
			</p>
			<p class="error"></p>
		</dd>
	</dl>
	<dl>
		<dt>文章排序：</dt>
		<dd>
			<input id="sort" type="number" class="input-common harf" min="0" onkeyup="value=value.replace(/[^\d+(\.\d+)?]/g,'')"/>
			<p class="error"></p>
		</dd>
	</dl>
	<dl>
		<dt>文章点击量：</dt>
		<dd>
			<input id="click" type="number" class="input-common harf" min="0" onkeyup="value=value.replace(/[^\d+(\.\d+)?]/g,'')"/><em class="unit">次</em>
			<p class="error"></p>
		</dd>
	</dl>
	<dl>
		<dt>文章评论数：</dt>
		<dd>
			<input id="comment_count" class="input-common harf" type="number" min="0" onkeyup="value=value.replace(/[^\d+(\.\d+)?]/g,'')"/><em class="unit">次</em>
			<p class="error"></p>
		</dd>
	</dl>
	<dl>
		<dt>文章分享数：</dt>
		<dd>
			<input id="share_count" class="input-common harf" type="number" min="0" onkeyup="value=value.replace(/[^\d+(\.\d+)?]/g,'')"/><em class="unit">次</em>
			<p class="error"></p>
		</dd>
	</dl>
	<dl>
		<dt></dt>
		<dd>
			<button class="btn-common btn-big" onclick="save(1)" >保存</button>
			<!-- <button class="btn-common btn-big" onclick="save(2)" >保存并预览</button> -->
			<button class="btn-common-cancle btn-big" onclick="window.location.href = window.history.go(-1);" >返回</button>
		</dd>
	</dl>
</div>

{/block}
{block name="script"}
<script src="__STATIC__/js/ajax_file_upload.js" type="text/javascript"></script>
<script src="__STATIC__/js/file_upload.js" type="text/javascript"></script>
<script src="__STATIC__/kindeditor/kindeditor-min.js" charset="utf-8"></script>
<script src="__STATIC__/kindeditor/lang/zh_cn.js" charset="utf-8"></script>
<script type="text/javascript">

var ue = UE.getEditor('editor',{
	toolbars : [
		[ 'source', // 源代码
		'bold', // 加粗
		'italic', // 斜体
		'underline', // 下划线
		'strikethrough', // 删除线
		'forecolor', // 字体颜色
		'backcolor', // 背景色
		'fontfamily', // 字体
		'fontsize', // 字号
		'paragraph', // 段落格式
		'justifyleft', // 居左对齐
		'justifycenter', // 居中对齐
		'justifyright', // 居右对齐
		'superscript', // 上标
		'blockquote', // 引用
		'selectall', // 全选
		'preview', // 预览
		'removeformat', // 清除格式
		'inserttable', // 插入表格
		'unlink', // 取消链接
		'insertrow', // 前插入行
		'insertcol', // 前插入列
		'mergeright', // 右合并单元格
		'mergedown', // 下合并单元格
		'deleterow', // 删除行
		'deletecol', // 删除列
		'splittorows', // 拆分成行
		'splittocols', // 拆分成列
		'splittocells', // 完全拆分单元格
		'deletecaption', // 删除表格标题
		'inserttitle', // 插入标题
		'mergecells', // 合并多个单元格
		'deletetable', // 删除表格
		'cleardoc', // 清空文档
		 'simpleupload', // 单图上传
		'insertimage', // 多图上传
		'link', // 超链接
		'emotion', // 表情
		'insertorderedlist', // 有序列表
		'insertunorderedlist', // 无序列表
		'fullscreen', // 全屏
		'directionalityltr', // 从左向右输入
		'directionalityrtl', // 从右向左输入
		'imagenone', // 默认
		'imageleft', // 左浮动
		'imageright', // 右浮动
		// 'attachment', // 附件
		'imagecenter', // 居中
		'lineheight', // 行间距
		'customstyle', // 自定义标题
		]]
});

$(function(){
	UE.getEditor('editor').addListener( 'ready', function() {
		if($("#content").val()){
			UE.getEditor('editor').setContent($("#content").val());
		}
	} );
})

function validation(){
	var title=$("#title");//文章标题
	var class_id=$("#class_id");//所属分类
	if(title.val() == ""){
		title.next().css("display","inline-block");
		title.focus();
		return false;
	}else{
		$('.error').hide();
	}
	
	if(class_id.val() == 0){
		class_id.next().css("display","inline-block");
		class_id.focus();
		return false;
	}else{
		$('.error').hide();
	}
	return true;
}

var flag = false;//防止重复提交
function save(type){
	if(!validation()){
		return;
	}
	var comment_flag = $("#comment_flag").prop('checked') ? 1 : 0 ;
	var commend_flag = $("#commend_flag").prop('checked') ? 1 : 0 ;
	var title=$("#title").val();
	var class_id=$("#class_id").val();
	var short_title=$("#short_title").val();
	var source=$("#source").val();
	var url=$("#url").val();
	var author=$("#author").val();
	var summary=$("#summary").val();
	var content = UE.getEditor('editor').getContent();
	if(content.length>30000){
		showTip("文章内容太长","warning");
		return;
	}
	var image=$("#title_img").val();
	var keyword=$("#keyword").val();
	var article_id_array=$("#article_id_array").val();
	var click=$("#click").val();
	var sort=$("#sort").val();
	var tag=$("#tag").val();
	var comment_count=$("#comment_count").val();
	var share_count=$("#share_count").val();
	var attachment_path = $("#attachment").val();
	
	if(flag){
		return;
	}
	flag = true;
	$.ajax({
		type:"post",
		url:"{:__URL('ADMIN_MAIN/Cms/addArticle')}",
		data:{
			'title':title,
			'class_id':class_id,
			'short_title':short_title,
			'source':source,
			'url':url,
			'author':author,
			'summary':summary,
			'content':content,
			'image':image,
			'keyword':keyword,
			'article_id_array':article_id_array,
			'click':click,
			'sort':sort,
			'commend_flag':commend_flag,
			'comment_flag':comment_flag,
			'attachment_path':attachment_path,
			'tag':tag,
			'comment_count':comment_count,
			'share_count':share_count
		},
		success:function(data){
			if (data["code"] > 0) {
				showTip(data["message"],'success');
				if(type==1){
					location.href=__URL("ADMIN_MAIN/cms/articlelist");
				}
				if(type==2){
					window.open(__URL("SHOP_MAIN/cms/articleclassinfo?article_id="+data['code']));
				}
			}else{
				showTip(data["message"],'error');
				flag = false;
			}
		}
	});
}

function imgUpload(event) {
	var fileid = $(event).attr("id");
	var data = new Object();
	var id = $(event).next().attr("id");
	var only_type = $(event).attr('only-type');
	if(!only_type){
		data.file_path = UPLOADCOMMON;
	}else{
		data.file_path = UPLOADFILE;
	}
	uploadFile(fileid,data,function(res){
		
		if(res.code){
			$("#" + id).val(res.data);
			$("#text_" + id).val(res.data);
			$("#preview_"+ id).attr("data-src",__IMG(res.data));
			showTip(res.message,"success");
		}else{
			showTip(res.message,"error");
		}
	});
}

/**
 * 删除文件
 * @param {Object} file_url
 * @param {Object} even
 */
function delete_file(file_url,even){
	$.ajax({
		type:"post",
		url:"{:__URL('ADMIN_MAIN/system/delete_file')}",
		data:{'file_url':file_url},
		dataType:'json',
		success:function(data){
			if (data['code']>0) {
				$( "#dialog" ).dialog({
					buttons: {
						"确定,#01B044": function() {
							$(this).dialog('close');
						}
					},
					contentText:"附件移除成功",
						time:1,
				});
				$(even).parent().empty();
			}else{
				$( "#dialog" ).dialog({
					buttons: {
						"确定,#0059d6,#fff": function() {
							$(this).dialog('close');
						}
					},
					contentText:data['message'],
				});
			}
		}
	});
}
</script>
{/block}